﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PoliAsis.Domain.Abstract
{
    public abstract class ControlObjectState<T> where T : class
    {
        public abstract Validator validator { get; set; }

        public virtual bool OnBeforeSave(T obj)
        {
            OnValidate(obj);
            return true;
        }
        public virtual bool OnBeforeSave(List<T> obj)
        {
            return true;
        }
        public virtual bool OnBeforeUpdate(T obj)
        {
            OnValidate(obj);
            return true;
        }
        public virtual bool OnBeforeDelete(T obj)
        {
            return true;
        }

        /// <summary>
        /// always at the end of the child OnValidate Method, return base.OnValidate
        /// to process the validator state and to throw the message
        /// </summary>
        /// <param name="obj"></param>
        public virtual void OnValidate(T obj)
        {
            if (!validator.IsValid)
            {
                throw new Exception(validator.GetValidationMessage());
            }
        }
    }
}
